using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace WowVoiceBox
{
    public static class LogFile
    {
        static string logFile = "logfile.log";
        static string errorFile = "error.log";

        static StreamWriter EventLog;
        static StreamWriter ErrorLog;

        public enum LogMsgType
        {
            EventMsg,
            ErrorMsg
        }

        public static bool InitLogs(bool clear)
        {            
            try
            {
                EventLog = new StreamWriter(Application.LocalUserAppDataPath + "\\" + logFile, !clear);
                ErrorLog = new StreamWriter(Application.LocalUserAppDataPath + "\\" + errorFile, !clear);
                EventLog.AutoFlush = true;
                ErrorLog.AutoFlush = true;
            } catch (Exception e) {
                MessageBox.Show("Error Initializing Log Files. Error message: " + e.ToString(), "LOG FILE ERROR!");
                return false;
            }

            PostMessage("Log Files Initialized.", LogMsgType.EventMsg);
            return true;
        }

        public static void PostMessage(string message)
        {
            PostMessage(message, LogMsgType.EventMsg);
        }

        public static void PostMessage(string message, LogMsgType type)
        {
            string msgStr = DateTime.Now.ToString() + " - " + message;

            if (type == LogMsgType.EventMsg)
                EventLog.WriteLine(msgStr);
            else
                ErrorLog.WriteLine(msgStr);
        }

        public static void CloseLogs()
        {
            PostMessage("Logs closing");

            if (EventLog != null)
                EventLog.Close();

            if (ErrorLog != null)
                ErrorLog.Close();
        }
    }
}
